package com.example.feidaolumanager.componant

import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.example.feidaolumanager.api.ApiService
import com.example.feidaolumanager.viewModel.FileUploadViewModel

@Composable
fun MachineImageItem(
    activity: ComponentActivity,
    hint: String,
    machineImageUrl: String?,
    imageHeight: Dp,
    index: Int,
    onImageClick: (String) -> Unit,
    onImageUploaded: (String) -> Unit,
    fileUploadViewModel: FileUploadViewModel = viewModel()
) {
    fileUploadViewModel.uploadImagePath.observe(activity) {
        if (it != null) {
            if (fileUploadViewModel.currentUploadImageIndex == index) {
                // 上传的图片是为我上传的
                onImageUploaded(it)
            }
        }
    }


    val launcher =
        rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->

            // TODO: 上传选择的图片到服务器

            if (uri != null) {
                // 获取选择的图片的输入流
                val inputStream = activity.contentResolver.openInputStream(uri)
                // 获取选择图片的类型: "image/jpeg"
                val type = activity.contentResolver.getType(uri)

                val imageBytes = inputStream?.readBytes()
                inputStream?.close()
                if (imageBytes != null) {
                    fileUploadViewModel.fileUpload(
                        imageBytes,
                        type?.replace("/", ".")
                    )
                }
            }
        }

    if (machineImageUrl.isNullOrEmpty()) {
        Column(
            modifier = Modifier.height(imageHeight),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Bottom
        ) {
            Icon(
                imageVector = Icons.Default.Add,
                contentDescription = "点击上传图片",
                modifier = Modifier
                    .clickable {
                        // 点击选择图片上传
                        fileUploadViewModel.currentUploadImageIndex = index
                        launcher.launch("image/*")
                    }
                    .size(80.dp)
                    .border(width = 1.dp, color = Color.Black)
            )
            Text(text = hint)
        }

    } else {
        AsyncImage(
            modifier = Modifier
                .padding(all = 10.dp)
                .clickable {
                    onImageClick(ApiService.BASE_URL + machineImageUrl)
                }
                .height(imageHeight),
            model = ApiService.BASE_URL + machineImageUrl,
            contentDescription = "机械外观",
            contentScale = ContentScale.FillBounds
        )
    }
}